package net.maku.express.biza.controller.packagecollect;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.express.biza.controller.collectExpress.vo.CollectExpressSelectiveVO;
import net.maku.express.biza.controller.packagecollect.vo.*;
import net.maku.express.biza.service.packagecollect.PackageCollectService;
import net.maku.express.biza.vo.PackageCollectVO;
import net.maku.framework.common.utils.Result;
import net.maku.framework.common.vo.IdsVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author
 * @Description: 散件合包
 * @Date
 */
@RequestMapping("packCollect")
@RestController
@Tag(name="散件合包")
@AllArgsConstructor
public class PackageCollectController {


    private final PackageCollectService packageCollectService;

    @PostMapping("/select")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<Page<PackageCollectPageVO>> select(@RequestBody @Valid PackageCollectSelectRequestVO packageCollectRequestVO){
        return packageCollectService.select(packageCollectRequestVO);
    }

    @PostMapping("/selectAdd")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<List<PackageCollectPageVO>> selectDD(@RequestBody @Valid PackageCollectSelectAddRequestVO packageCollectRequestVO){
        return packageCollectService.select1(packageCollectRequestVO);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    @PreAuthorize("hasAuthority('express:package:add')")
    public Result<String> add(@RequestBody @Valid PackageCollectVO packageCollectVO){
        return packageCollectService.addData(packageCollectVO);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('express:collect:delete')")
    public Result<Integer> delete(@RequestBody @Valid JSONObject jsonObject){
        if(!jsonObject.containsKey("id")){
            return Result.error("缺少id参数!");
        }
        Long id = jsonObject.getLong("id");
        return packageCollectService.deleteData(id);
    }


    @PostMapping("/update")
    @Operation(summary = "更新")
    @PreAuthorize("hasAuthority('express:collect:update')")
    public Result<Integer> update(@RequestBody @Valid PackageCollectVO packageCollectVO){
        return packageCollectService.updateData(packageCollectVO);
    }

    @GetMapping("/selectBoxNO")
    @Operation(summary = "箱号")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<String> getBoxNO(){
        return packageCollectService.getBoxNO();
    }

    @PutMapping("/updateBoxState")
    @Operation(summary = "装箱状态")
    @PreAuthorize("hasAuthority('express:package:update')")
    public Result<Integer> updateBoxState(@RequestBody @Valid PackageCollectStatusRequestVO statusRequestVO){
        return packageCollectService.updateBoxState(statusRequestVO);
    }

    @PutMapping("/updateOutboundState")
    @Operation(summary = "出仓状态")
    @PreAuthorize("hasAuthority('express:package:update')")
    public Result<List<CollectExpressSelectiveVO>> updateOutboundState(@RequestBody @Valid  IdsVO idsVO){
        return packageCollectService.updateOutboundStateByIds(idsVO.getIds());
    }

    @PostMapping("/selectOne")
    @Operation(summary = "详情")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<PackageCollectVO> selectOne(@RequestBody @Valid JSONObject jsonObject){
        if(!jsonObject.containsKey("id")){
            return Result.error("缺少id参数!");
        }
        Long id = jsonObject.getLong("id");
        return packageCollectService.selectOne(id);
    }

    @GetMapping("/selectOrder")
    @Operation(summary = "订单详情")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<List<PackageCollectOrderVO>> selectOrder(@Valid Long id){
        return packageCollectService.selectOrder(id);
    }

    @PostMapping("/selectPrint")
    @Operation(summary = "订单详情")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<PackageCollectPageVO> selectPrint(@RequestBody @Valid JSONObject jsonObject){
        if(!jsonObject.containsKey("id")){
            return Result.error("缺少id参数!");
        }
        Long id = jsonObject.getLong("id");
        return packageCollectService.selectPrintData(id);
    }

    @PostMapping("/outWarehouse")
    @Operation(summary = "出仓")
    @PreAuthorize("hasAuthority('express:package:add')")
    public Result<Integer> outWarehouse(@RequestBody List<Long> idList){
        return packageCollectService.outWarehouse(idList);
    }

}
